<template>
  <a-modal
    :title="title"
    :width="720"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    :destroyOnClose="true"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="页面数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number
            :min="1"
            v-decorator="['num', {initialValue: 1, rules: [{required: true, message: '请输入页面数量'}]}]"
          />
        </a-form-item>

        <a-form-item label="目录地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <ul  class='file-list-ul'>
            <a-tooltip  placement='topLeft' title='点击返回'>
              <li @click='handleBack' class='file-item' style='width: 100%'><a-icon class='li-icon' type='folder-open'></a-icon> <span v-if='search.name'>{{search.name}}</span><span v-else>/</span></li>
            </a-tooltip>
          </ul>
        </a-form-item>
        <a-form-item label="子目录" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <ul  class='file-list-ul sub-file-list'>
            <a-tooltip v-for='(item, index) in dirList'
                       :key='index' placement='topLeft' :title='item.label'>
              <li class='file-item oneline-hide' @click='handleChange(item.value)'>
                <a-icon class='li-icon' type='folder'></a-icon>
                {{ item.label }}
              </li>
            </a-tooltip>
            <li v-if='!dirList.length' style='color:red;'>无子目录</li>
          </ul>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import _ from 'lodash'
import { resIndex } from '@/api/book/groupFile'
import { batchAddNullPage } from '@/api/book/page'
export default {
  components: {
  },
  data () {
    return {
      // 对话框标题
      title: '批量添加空页面',
      // 标签布局属性
      labelCol: { span: 5 },
      // 输入框布局属性
      wrapperCol: {
        span: 13
      },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 当前记录
      search:{name:''},
      bookId:'',
      actionType:'',
      dirList:[],
    }
  },
  methods: {
    /**
     * 显示对话框
     */
    open (bookId,type) {
      // 显示窗口
      this.visible = true
      this.bookId = bookId
      this.actionType = type
      this.targetId = ''
      this.search = {name:''}
      this.dirList=[]
      this.getDirOptions()
    },

    // 获取文件分组列表
    getDirOptions() {
      this.isLoading=true
      resIndex(this.search).then(result => {
        this.dirList = result.data
      }).finally(() => {
        this.isLoading = false
      })
    },
    handleBack(){
      const dir=/\//.test(this.search.name)?'/':'\\'
      const value = this.search.name.split(dir)
      value.pop()
      if(value[0] === ''){
        value.splice(0,1)
      }
      this.search.name = value.join(dir)
      this.getDirOptions()
    },
    handleChange(value){
      this.search.name=value
      this.getDirOptions()
    },


    /**
     * 确认按钮
     */
    handleSubmit (e) {
      e.preventDefault()
      // 表单验证
      const { form: { validateFields } } = this
      validateFields((errors, values) => {
        // 提交到后端api
        if (!errors) {
          if(!this.search.name){
            this.$message.error('请选择目录', 0.8)
            return;
          }
          values.dir = this.search.name
          this.onFormSubmit(values)
        }
      })
    },

    /**
     * 关闭对话框事件
     */
    handleCancel () {
      this.visible = false
    },

    /**
     * 提交到后端api
     */
    onFormSubmit (values) {
      this.confirmLoading = true
      values.bookId = this.bookId
      values.actionType = this.actionType
      batchAddNullPage(values)
        .then((result) => {
          // 显示成功
          this.$message.success(result.message, 1.5)
          // 关闭对话框事件
          this.handleCancel()
          // 通知父端组件提交完成了
          this.$emit('handleSubmit', values)
        })
        .finally((result) => {
          this.confirmLoading = false
        })
    },
    onForceUpdate (bool = false) {
      this.$forceUpdate()
      // bool为true时再执行一次 $forceUpdate, 特殊情况下需执行两次，原因如下：
      // 第一次执行 $forceUpdate时, 新元素绑定v-decorator无法获取到form.getFieldValue
      bool && setTimeout(() => {
        this.$forceUpdate()
      }, 10)
    },

  }
}
</script>
<style lang='less' scoped>
// 文件列表
  .file-list-ul {
    margin: 0;
    padding: 0;
    overflow-y: scroll;
    &.sub-file-list{
      height: 300px;
    }
    //display: flex;
    //flex-direction: column;
    //flex-flow: row wrap;
  }

  .file-item {
    width: 33%;
    float: left;
    //flex: 0 0 33%;
    //flex-grow: 0;
    cursor: pointer;
    //height: 40px;
    padding: 5px 10px;
    -webkit-transition: All 0.2s ease-in-out;
    -moz-transition: All 0.2s ease-in-out;
    -o-transition: All 0.2s ease-in-out;
    transition: All 0.2s ease-in-out;
    border: 1px solid transparent;
    &.selected{
      color: #fff;
      background-color:  #16bce2;
      border-color: #fff;
    }

    .li-icon {
      font-size: 15px;
    }

    &:hover {
      border-color: #16bce2;
    }
  }

</style>